import os
import re
import sqlite3
# commit()--事务提交
# rollback()--事务回滚
# close()--关闭一个数据库连接
# cursor()--创建一个游标

# '评论人': user_name,
# '评论头像': user_face,
# '评论时间': user_date,
# '评论内容': user_text,
# '购买商品类型': info,
# '评论视频截图': videoimg,
# '评论视频': videoUrl,
# '好评图片': imglist,

#创建表
from web.settings import BASE_DIR
class tbdb:
    def __init__(self):
        print('tb数据库')
    def initdb(self,cursor):
        create_table_sql = '''create table IF NOT EXISTS happy(
                            'ID' INTEGER,
                            '评论人' text,
                            '评论头像' text,
                            '评论时间' text,
                            '评论内容' text unique,
                            '购买商品类型' text,
                            '评论视频截图' text,
                            '评论视频' text,
                            '好评图片' text
                            )'''
        cursor.execute(create_table_sql)  # 执行这个语句

    # 增
    def insertdb(cursor, id, a, b, c, d, e, f, g, h):
        # 二、插入数据：执行语句即可
        # conn = sqlite3.connect("tb.db")
        # cursor=conn.cursor()
        username = "ytouch"
        password = "123456"
        user_id = 1

        insert_data_sql = '''insert or ignore into
                        happy(
                            ID,
                            评论人,
                            评论头像,
                            评论时间,
                            评论内容,
                            购买商品类型,
                            评论视频截图,
                            评论视频,
                            好评图片
                        )
                        values (
                        :shangpinid,
                        :pinglunren,
                        :pingluntaouxiang,
                        :pinglunshijian,
                        :pinglunneirong,
                        :shangpinleixing,
                        :pinglunshipinjietu,
                        :pinglunshipin,
                        :haopingtupian
                        )'''
        cursor.execute(insert_data_sql,
                       {
                           'shangpinid': id,
                           'pinglunren': a,
                           'pingluntaouxiang': b,
                           'pinglunshijian': c,
                           'pinglunneirong': d,
                           'shangpinleixing': e,
                           'pinglunshipinjietu': f,
                           'pinglunshipin': g,
                           'haopingtupian': h
                       })

    # 把这两个函数不能直接用,把他门直接复制到想使用的地方
    def opendb(self):
        print('123')
        dbpath = os.path.join(BASE_DIR, 'templates\\userdb.db')
        conn = sqlite3.connect(dbpath)
        cursor = conn.cursor()
        return cursor,conn,

    def closedb(self,conn, cursor):
        conn.commit()
        cursor.close()



if __name__ == '__main__':
    db = tbdb()
    cour,conn = db.opendb()
    db.initdb(cour)
    db.closedb(conn,cour)

    msg='或復·制这段描述＄dQS5YTxadtL＄后到淘♂寳♀'


    def match_kl(msg):  # \x{ffe5}([a-zA-Z0-9]{11})\x{ffe5}#isu
        # 按行读取字符集txt文件
        def readtxt():
            list = []
            try:
                for line in open('淘口令字符集合.txt',encoding="utf-8",errors='ignore'):
                    # 清洗空格后如果不为空就下一步处理
                    if line.strip() != '':
                        # print(f"本行内容为{line}")
                        # 每行的数据都需要清洗空格后进行下一步处理
                        abc = line.strip()
                        list.append(abc)
                    else:
                        print("本行为空")
                        continue
            except Exception as e:
                print(e)
            return list

        list = readtxt()
        print(list)
        matchstr = ''
        for i in list:
            if list.index(i) == 0:
                matchstr = matchstr + i
            else:
                matchstr = matchstr + '|' + i
        matchtkl = fr'(?:{matchstr})\S{{11}}(?:{matchstr})'

        print(matchtkl)
        tkl = re.compile(matchtkl)
        tkllist = re.findall(tkl, msg)
        # 如果匹配为淘口令：
        if tkllist:
            print(tkllist[0])
            return tkllist[0]


    match_kl(msg)



